<?php
  header('Content-Type:text/html;charset=utf-8');
  class PDOc{
  	public static $link=null;

  	public static function hwd(){
  		echo "hallo world!";
  	}

  	public static function _connect(){  //链接数据库
  		try{
  		  self::$link=new PDO('mysql:host=localhost;dbname=dtb','root','');
      }catch(PDOException $e){
  		  self::throwException($e->getMessage()); 
      }
    }

  	public static function throwException($info=''){  //抛出异常
  		echo '<br/>'.$info.'<br/>';
  	}

  	public static function PDOupdate($table,$update,$limit){  //update的函数
  		if ($table=='' or $update=='' or $limit=='') {
  			throwException('PDOupdate参数错误');
  			return;
  		} else {
  		  return 'UPDATE '.$table.' SET '.$update.' WHERE '.$limit.';';
  	  }
  	}

  	public static function PDOinsert($tableParam,$tableValue,$table){  //insert的函数
      if (($table==null) OR (!self::checkTableExistence($table))) {
  			 self::throwException('表格不存在！');
  			 return null;
  		}
  	  return "INSERT INTO ".$table."(".$tableParam.") VALUES(".$tableValue.");";
  	}

    public static function PDOdelete($table,$limit){   //delete的函数
      if (($table==null) OR (!self::checkTableExistence($table))) {
           self::throwException('表格不存在！');
           return null;
        }
        return "DELETE FROM ".$table." WHERE ".$limit.";";
    }

  	public static function PDOselect($obj=null,$limit=null,$table=null)  //select 的函数
  	{
  		if (($table==null) OR (!self::checkTableExistence($table))) {
  			self::throwException('表格不存在！');
  			return null;
  		}
  		if ($obj==null) {
  			self::throwException('对象不能为空！');
  			return null;
  		} else {
  			if ($limit==null) {
  				return "SELECT ".$obj." FROM ".$table.";";
  			} else {
  				return "SELECT ".$obj." FROM ".$table." WHERE ".$limit.";";
  			}
  		}
  	}

    public static function PDOselectByOrder($obj=null,$limit=null,$table=null,$orderBy)
    //自带排序的select，虽然可以和上面合并，但是太多if看着眼花就算了
    {
      if (($table==null) OR (!self::checkTableExistence($table))) {
        self::throwException('表格不存在！');
        return null;
      }
      if ($obj==null) {
        self::throwException('对象不能为空！');
        return null;
      } else {
        if ($limit==null) {
          return "SELECT ".$obj." FROM ".$table." ORDER BY ".$orderBy.";";
        } else {
          return "SELECT ".$obj." FROM ".$table." WHERE ".$limit." ORDER BY ".$orderBy.";";
        }
      }
    }

  	public static function login($username='',$password=''){ 
  		if (($username=='') or ($password=='')) {
  			self::throwException('用户名或密码不能为空！');
  			self::Button("index.php","返回登录页面");
  		} else {
  			//var_dump(self::checkUserExistence($username));
        $password=self::encodePassword($username,$password);
  			if (self::checkUserExistence($username)) {
  				if (self::checkLoginInfo($username,$password)) {
  					echo '登录成功。<br/>';
            self::getUserInfo($username);
  					self::Button("userIndex.php","进入个人主页");
  				} else {
  					self::throwException('用户名或密码错误，请检查后再进行登录。');
  					self::Button("index.php","返回登录页面");
  				}
  			}else{
  				self::throwException('不存在该用户。');
  				self::Button("index.php","返回登录页面");
  		    }
  	    }
  	}

  	public static function register($username,$password){ 
      //可以内嵌写个函数用正则表达式检测敏感词汇，不过没有词库就作罢了
      //特别要完全筛选出含逻辑运算符的字符串，经测试用户名带or的不能正常注册及登录
  		if (($username=='') or ($password=='')) {
  			self::throwException('用户名或密码不能为空！');
  			self::Button("register.php","返回注册页面");
  		} else {
            if (self::checkUserExistence($username)) {
  				self::throwException('该用户名已被注册。');
  				self::Button("register.php","返回注册页面");
  			}else{
          $password=self::encodePassword($username,$password);
  				$tableParam="username,password,email,headPic,adminRank";
  				$tableValue="'{$username}','{$password}','','',0";
  				$table="user";
  				$sql=self::PDOinsert($tableParam,$tableValue,$table);
  				$res=self::$link->exec($sql);
  				if ($res<>0) {
  					echo '注册成功！<br/>';
  					self::Button("index.php","进入登录页面");
  				} else {
  					self::throwException('注册失败：未知异常');
  					self::Button("register.php","返回注册页面");
  				}
  		    }	
  		}
  	}

    public static function encodePassword($username,$password){
      return md5($username).md5($password);
    }

    public static function showHeadPic($id){
      if (self::checkHeadPicExistence($id)) {
        $obj='*';
        $limit='id='.$id;
        $table='user';
        $sql=self::PDOselect($obj,$limit,$table);
        $stmt=self::$link->query($sql);
        foreach($stmt as $row)
        {
          echo '<img src="headPic/'.$row['headPic'].'" width="100" height="100"><br/>';
        }
      } else {
        echo '<img src="headPic/defaut.jpg" width="100" height="100"><br/>';
      }
    }
    

    public static function newPassage($authorID,$authorAdminRank,$authorName,$title,$mainText,$createDate){     
      $tableParam='authorID,authorAdminRank,authorName,title,mainText,createDate';
      $tableValue="'{$authorID}','{$authorAdminRank}','{$authorName}','{$title}','{$mainText}','{$createDate}'";
      $table='passage';
      if ($title=='' or $mainText=='') {
        self::throwException('文章标题和正文内容不得为空！<br/>');
        self::Button('userIndex.php','回到主页');
        return;
      }
      $sql=self::PDOinsert($tableParam,$tableValue,$table);
      //var_dump($sql);
      $res=self::$link->exec($sql);
      if ($res==1) {
        echo "发布成功！<br/>";
        self::Button('userIndex.php','回到主页');
      } else {
        self::throwException('发生错误，res值为'.$res.'<br/>');
        self::Button('userIndex.php','回到主页');
      }
    }

    public static function super_searchPassage($pattern){
      $obj='*';
      //var_dump($pattern);
      $limit="title REGEXP '".$pattern."'";
      $table='passage';
      $orderBy='id desc';
      $sql=self::PDOselectByOrder($obj,$limit,$table,$orderBy);
      //var_dump($sql);
      $stmt1=self::$link->query($sql);
      $limit="mainText REGEXP '".$pattern."'";
      $sql=self::PDOselectByOrder($obj,$limit,$table,$orderBy);
      $stmt2=self::$link->query($sql);
      $limit="authorName REGEXP '".$pattern."'";
      $sql=self::PDOselectByOrder($obj,$limit,$table,$orderBy);
      $stmt3=self::$link->query($sql);
      if (($stmt1->rowCount()==0) AND ($stmt2->rowCount()==0) AND ($stmt3->rowCount()==0)) {
        self::throwException('无查询结果！');
      } else { //我寻思过写个函数输出表格，但是这个带form的实在不太好搞...
         echo '<table><tr><th>文章ID</th><th>作者</th><th>标题</th><th>最后更新日期</th><th>匹配原因</th><th>删除</th></tr>';
         if ($stmt1->rowCount()<>0)
            foreach ($stmt1 as $row) {
              echo '<tr><th>'.$row['id'].'</th><th>'.$row['authorName'].'</th><th>';
              echo '<form method="post" action="viewPassage.php"><input type="hidden" name="authorID" value='.$row['authorID'].'><input type="hidden" name="passageID" value='.$row['id'].'><input type="hidden" name="authorAdminRank" value='.$row['authorAdminRank']."><input type=submit value='{$row['title']}'></form>";
              echo '</th><th>'.$row['createDate'].'</th><th>标题</th>';
              echo '<th><form method="post" action="confirmDeletePassage.php"><input type="hidden" name="passageID" value='.$row['id'].'><input type=submit value="X"></form></th></tr>';
            }
        if ($stmt2->rowCount()<>0)
          foreach ($stmt2 as $row) {
            echo '<tr><th>'.$row['id'].'</th><th>'.$row['authorName'].'</th><th>';
            echo '<form method="post" action="viewPassage.php"><input type="hidden" name="authorID" value='.$row['authorID'].'><input type="hidden" name="passageID" value='.$row['id'].'><input type="hidden" name="authorAdminRank" value='.$row['authorAdminRank']."><input type=submit value='{$row['title']}'></form>";
            echo '</th><th>'.$row['createDate'].'</th><th>正文内容</th>';
            echo '<th><form method="post" action="confirmDeletePassage.php"><input type="hidden" name="passageID" value='.$row['id'].'><input type=submit value="X"></form></th></tr>';
          }
        if ($stmt3->rowCount()<>0)
          foreach ($stmt3 as $row) {
            echo '<tr><th>'.$row['id'].'</th><th>'.$row['authorName'].'</th><th>';
            echo '<form method="post" action="viewPassage.php"><input type="hidden" name="authorID" value='.$row['authorID'].'><input type="hidden" name="passageID" value='.$row['id'].'><input type="hidden" name="authorAdminRank" value='.$row['authorAdminRank']."><input type=submit value='{$row['title']}'></form>";
            echo '</th><th>'.$row['createDate'].'</th><th>作者</th>';
            echo '<th><form method="post" action="confirmDeletePassage.php"><input type="hidden" name="passageID" value='.$row['id'].'><input type=submit value="X"></form></th></tr>';
         }
        echo '</table>';
      }
    }
    

    public static function searchPassage($pattern){   //是上面的复读，砍掉了删除键，给一般用户用
      $obj='*';
      $limit="title REGEXP '".$pattern."'";
      $table='passage';
      $orderBy='id desc';
      $sql=self::PDOselectByOrder($obj,$limit,$table,$orderBy);
      //var_dump($pattern);
      $stmt1=self::$link->query($sql);
      $limit="mainText REGEXP '".$pattern."'";
      $sql=self::PDOselectByOrder($obj,$limit,$table,$orderBy);
      $stmt2=self::$link->query($sql);
      $limit="authorName REGEXP '".$pattern."'";
      $sql=self::PDOselectByOrder($obj,$limit,$table,$orderBy);
      $stmt3=self::$link->query($sql);
      if (($stmt1->rowCount()==0) AND ($stmt2->rowCount()==0) AND ($stmt3->rowCount()==0)) {
        self::throwException('无查询结果！');
      } else {
         echo '<table><tr><th>文章ID</th><th>作者</th><th>标题</th><th>最后更新日期</th><th>匹配原因</th></tr>';
         if ($stmt1->rowCount()<>0)
           foreach ($stmt1 as $row) {
             echo '<tr><th>'.$row['id'].'</th><th>'.$row['authorName'].'</th><th>';
             echo '<form method="post" action="viewPassage.php"><input type="hidden" name="authorID" value='.$row['authorID'].'><input type="hidden" name="passageID" value='.$row['id'].'><input type="hidden" name="authorAdminRank" value='.$row['authorAdminRank']."><input type=submit value='{$row['title']}'></form>";
             echo '</th><th>'.$row['createDate'].'</th><th>标题</th></tr>';
           }
        if ($stmt2->rowCount()<>0)
          foreach ($stmt2 as $row) {
            echo '<tr><th>'.$row['id'].'</th><th>'.$row['authorName'].'</th><th>';
            echo '<form method="post" action="viewPassage.php"><input type="hidden" name="authorID" value='.$row['authorID'].'><input type="hidden" name="passageID" value='.$row['id'].'><input type="hidden" name="authorAdminRank" value='.$row['authorAdminRank']."><input type=submit value='{$row['title']}'></form>";
            echo '</th><th>'.$row['createDate'].'</th><th>正文内容</th></tr>';
          }
        if ($stmt3->rowCount()<>0)
          foreach ($stmt3 as $row) {
            echo '<tr><th>'.$row['id'].'</th><th>'.$row['authorName'].'</th><th>';
            echo '<form method="post" action="viewPassage.php"><input type="hidden" name="authorID" value='.$row['authorID'].'><input type="hidden" name="passageID" value='.$row['id'].'><input type="hidden" name="authorAdminRank" value='.$row['authorAdminRank']."><input type=submit value='{$row['title']}'></form>";
            echo '</th><th>'.$row['createDate'].'</th><th>作者</th></tr>';
          }
        echo '</table>';
      }
    }

    public static function showMyPassages($authorID){
      $obj='*';
      $limit="authorID='{$authorID}'";
      $table='passage';
      $orderBy='id desc';
      $sql=self::PDOselectByOrder($obj,$limit,$table,$orderBy);
      $stmt=self::$link->query($sql);
      if ($stmt->rowCount()==0) {
        echo '<br/>你还没有写过任何文章！<br/>';
      } else {
        echo '<table><tr><th>文章ID</th><th>标题</th><th>最后更新日期</th><th>删除</th></tr>';
        foreach ($stmt as $row) {
          echo '<tr><th>'.$row['id'].'</th><th>';
          echo '<form method="post" action="viewPassage.php"><input type="hidden" name="authorID" value='.$row['authorID'].'><input type="hidden" name="passageID" value='.$row['id'].'><input type="hidden" name="authorAdminRank" value='.$row['authorAdminRank']."><input type=submit value='{$row['title']}'></form>";
          echo '</th><th>'.$row['createDate'].'</th>';
          echo '<th><form method="post" action="confirmDeletePassage.php"><input type="hidden" name="passageID" value='.$row['id'].'><input type=submit value="X"></form></th></tr>';
        }
        echo '</table>';
      }
    }

    public static function super_showAllPassages(){
      $obj='*';
      $limit="authorID>0";
      $table='passage';
      $orderBy='id desc';
      $sql=self::PDOselectByOrder($obj,$limit,$table,$orderBy);
      $stmt=self::$link->query($sql);
      if ($stmt->rowCount()==0) {
        echo '<br/>没有任何文章！<br/>';
      } else {
        echo '<table><tr><th>文章ID</th><th>作者</th><th>标题</th><th>最后更新日期</th><th>删除</th></tr>';
        foreach ($stmt as $row) {
          $authorName=$row['authorName'];
          echo '<tr><th>'.$row['id'].'</th><th>'.$authorName.'</th><th>';
          echo '<form method="post" action="viewPassage.php"><input type="hidden" name="authorName" value='.$row['authorName'].'><input type="hidden" name="passageID" value='.$row['id'].'><input type="hidden" name="authorID" value='.$row['authorID'].'><input type="hidden" name="authorAdminRank" value='.$row['authorAdminRank']."><input type=submit value='{$row['title']}'></form>";
          echo '</th><th>'.$row['createDate'].'</th>';
          echo '<th><form method="post" action="confirmDeletePassage.php"><input type="hidden" name="passageID" value='.$row['id'].'><input type=submit value="X"></form></th></tr>';
        }
        echo '</table>';
      }
    }

    public static function showAllPassages(){    //同样是砍掉了删除键
      $obj='*';
      $limit="authorID>0";
      $table='passage';
      $orderBy='id desc';
      $sql=self::PDOselectByOrder($obj,$limit,$table,$orderBy);
      $stmt=self::$link->query($sql);
      if ($stmt->rowCount()==0) {
        echo '<br/>没有任何文章！<br/>';
      } else {
        echo '<table><tr><th>文章ID</th><th>作者</th><th>标题</th><th>最后更新日期</th></tr>';
        foreach ($stmt as $row) {
          $authorName=$row['authorName'];
          echo '<tr><th>'.$row['id'].'</th><th>'.$authorName.'</th><th>';
          echo '<form method="post" action="viewPassage.php"><input type="hidden" name="authorName" value='.$row['authorName'].'><input type="hidden" name="passageID" value='.$row['id'].'><input type="hidden" name="authorID" value='.$row['authorID'].'><input type="hidden" name="authorAdminRank" value='.$row['authorAdminRank']."><input type=submit value='{$row['title']}'></form>";
          echo '</th><th>'.$row['createDate'].'</th></tr>';
        }
        echo '</table>';
      }
    }

    public static function showPassage($passageID){
      $obj='*';
      $limit='id='.$passageID;
      $table='passage';
      $sql=self::PDOselect($obj,$limit,$table);
      $stmt=self::$link->query($sql);
      if ($stmt->rowCount()==1) {
        foreach ($stmt as $row) {
          echo "标题：'{$row['title']}'<br/>";
          echo '<textarea readonly="readonly" rows="20" cols="60">'.$row['mainText'].'</textarea>';
        }
      } else {
        self::throwException('查询错误，结果行数为'.'<br/>');
        self::Button('userIndex.php','返回个人主页');
      }
    }


    public static function editPassage($passageID){
      $obj='*';
      $limit='id='.$passageID;
      $table='passage';
      $sql=self::PDOselect($obj,$limit,$table);
      $stmt=self::$link->query($sql);
      if ($stmt->rowCount()==1) {
        foreach ($stmt as $row) {
          echo "标题：'{$row['title']}'<br/>";
          echo '<form action="actionEditPassage.php" method="post">';
          echo '<input type="hidden" name="authorName" value='.$row['authorName'].'>';
          echo '<input type="hidden" name="authorID" value='.$row['authorID'].'>';
          echo '<input type="hidden" name="authorAdminRank" value='.$row['authorAdminRank'].'>';
          echo '<input type="hidden" name="id" value='.$row['id'].'>';
          echo "<input type=".'"hidden"'." name=".'"title"'." value='{$row['title']}'>";
          echo '<textarea rows="20" cols="60" name="mainText">'.$row['mainText'].'</textarea>';
          echo '<br/><input type="submit" value="提交修改">';
          echo '</form>';
        }
      } else {
        self::throwException('查询错误，结果行数为'.'<br/>');
        self::Button('userIndex.php','返回个人主页');
      }
    }

    public static function deletePassage($passageID){
      $sql=self::PDOdelete('passage',"id='{$passageID}'");
      $res=self::$link->exec($sql);
      if ($res==1) {
        //echo '删除成功';
        return true;
      } else {
        self::throwException('删除原文时出错，res值为：'.$res);
        return false;
      }
    } 

    public static function deleteUser($id){
      $sql=self::PDOdelete('user',"id='{$id}'");
      $res=self::$link->exec($sql);
      if ($res==1) {
        //echo '删除成功';
        return true;
      } else {
        self::throwException('删除用户时出错，res值为：'.$res);
        return false;
      }
    }

    public static function super_showAllUsers(){ //超管专属
      $obj='id,username,email,adminRank';
      $limit="adminRank<2";
      $table='user';
      $orderBy='id ASC';
      $sql=self::PDOselectByOrder($obj,$limit,$table,$orderBy);
      $stmt=self::$link->query($sql);
      if ($stmt->rowCount()==0) {
        echo '<br/>没有任何用户！<br/>';
      } else {
        echo '<table><tr><th>ID</th><th>用户名</th><th>邮箱地址</th><th>管理权限</th><th>删除</th></tr>';
        foreach ($stmt as $row) {
         echo '<tr><th>'.$row['id'].'</th><th>'.$row['username'].'</th><th>'.$row['email'].'</th><th>'.self::getAdminRank($row['adminRank']).'</th>';
         echo '<th><form method="post" action="confirmDeleteUser.php"><input type="hidden" name="id" value='.$row['id'].'><input type=submit value="X"></form></th></tr>';
        }
        echo '</table>';
      }
    }

    public static function showAllUsers(){ //管理员专属
      $obj='id,username,email,adminRank';
      $limit="adminRank<2";
      $table='user';
      $orderBy='id ASC';
      $sql=self::PDOselectByOrder($obj,$limit,$table,$orderBy);
      $stmt=self::$link->query($sql);
      if ($stmt->rowCount()==0) {
        echo '<br/>没有任何用户！<br/>';
      } else {
        echo '<table><tr><th>ID</th><th>用户名</th><th>邮箱地址</th><th>管理权限</th></tr>';
        foreach ($stmt as $row) {
         echo '<tr><th>'.$row['id'].'</th><th>'.$row['username'].'</th><th>'.$row['email'].'</th><th>'.self::getAdminRank($row['adminRank']).'</th></tr>';
        }
        echo '</table>';
      }
    }

  	public static function getUserInfo($username){  //将用户基本信息写入session
  		$obj='*';
  		$limit="username = '{$username}'";
  		$table="user";
  		$sql=self::PDOselect($obj,$limit,$table);
  		$stmt=self::$link->query($sql);
  		if ($stmt->rowCount()<>1) {
  			throwException('获取用户信息异常。');
  		} else {
  			session_start();
  			foreach ($stmt as $row) {
  				$_SESSION['username']=$row['username'];
  				$_SESSION['id']=$row['id'];
  				$_SESSION['adminRank']=$row['adminRank'];
  				$_SESSION['email']=$row['email'];
  			}
  		}
  	}

  	public static function checkLoginInfo($username='',$password=''){
  		$obj='*';
  		$limit="username = '{$username}'AND password='{$password}'";
  		$table="user";
  		$sql=self::PDOselect($obj,$limit,$table);
  		$res=self::$link->query($sql);
  		//var_dump($res->rowCount()==0);
  		if ($res->rowCount()==0) {
  			return false;
  		} else {
  			return true;
  		}
  	}


    public static function checkHeadPicExistence($id){
      $obj='*';
      $limit='id='.$id;
      $table='user';
      $sql=self::PDOselect($obj,$limit,$table);
      $stmt=self::$link->query($sql);
      foreach($stmt as $row)
      {
        if ($row['headPic']<>null) return true;
      }
      return false;
    }

  	public static function checkUserExistence($username=''){
  		//var_dump($username);
  		$sql=self::PDOselect("*",null,"user");
  		$stmt=self::$link->query($sql);
  		//foreach ($stmt as $row) echo $row['username'].'<br/>';
  		foreach ($stmt as $row) if ($row['username'] == $username) return true;
  		return false;
  	}

    public static function checkEmailExistence($email=''){
      //var_dump($username);
      $sql=self::PDOselect("*",null,"user");
      $stmt=self::$link->query($sql);
      //foreach ($stmt as $row) echo $row['username'].'<br/>';
      foreach ($stmt as $row) if ($row['email'] == $email) return true;
      return false;
    }

    public static function checkEmailValidity($subject=''){ //检查邮箱格式是否正确
      $pattern='/\w+@\w+\.\w+/';
      if (preg_match($pattern, $subject)) return true;else return false;
    }

    public static function checkIDExistence($id=''){
      $sql=self::PDOselect("*",null,"user");
      $stmt=self::$link->query($sql);
      foreach ($stmt as $row) if ($row['id'] == $id) return true;
      return false;
    }

  	public static function checkTableExistence($table){
  		$sql="SHOW TABLES;";
  		$stmt=self::$link->query($sql);
      foreach ($stmt as $row) if ($row['Tables_in_dtb']==$table) return true;
      return false;
  	}

    public static function updateEmail($id,$email){
    	$sql=self::PDOupdate('user',"email='{$email}'","id='{$id}'");
    	$res=self::$link->exec($sql);
    	if ($res==1) {
    		echo "邮箱修改成功！";
    		$_SESSION['email']=$email;
    		self::Button('userIndex.php','返回个人主页');
    	} else {
    		self::throwException('修改失败。res的值为'.$res);
    		self::Button('modifyEmail.php','返回修改页面');
    	}
    }

    public static function updateHeadPic($id,$filename){
      if (self::checkHeadPicExistence($id)) {
        $obj='*';
        $limit='id='.$id;
        $table='user';
        $sql=self::PDOselect($obj,$limit,$table);
        $stmt=self::$link->query($sql);
        foreach($stmt as $row)
        {
          $oldfile=$row['headPic'];
        }
        unlink('headPic/'.$oldfile);
      }
      $table='user';
      $update='headPic="'.$filename.'"';
      $limit='id='.$id;
      $sql=self::PDOupdate($table,$update,$limit);
      $res=self::$link->exec($sql);
      if ($res==1) {
        echo ' 数据库更新成功！<br/>';
      } else {
        self::throwException('数据库更新失败。res值为'.$res);
      }
    }

    public static function setAdmin($id){
      $table='user';
      $update='adminRank=1';
      $limit='id='.$id;
      $sql=self::PDOupdate($table,$update,$limit);
      $res=self::$link->exec($sql);
      if (self::checkIDExistence($id)){
        if ($res==1) {
          echo '成功设置管理员！<br/>';
          self::Button('viewAllUsers.php','返回');
        } else {
          self::throwException('设置管理员出错，res值为'.$res);
          self::Button('viewAllUsers.php','返回');
        }
      } else {
        self::throwException('不存在该用户。');
        self::Button('viewAllUsers.php','返回');
      }
    }

    public static function setNormalUser($id){
      $table='user';
      $update='adminRank=0';
      $limit='id='.$id;
      $sql=self::PDOupdate($table,$update,$limit);
      $res=self::$link->exec($sql);
      if (self::checkIDExistence($id)){
        if ($res==1) {
          echo '成功设置普通用户！<br/>';
          self::Button('viewAllUsers.php','返回');
        } else {
          self::throwException('设置普通用户出错，res值为'.$res);
          self::Button('viewAllUsers.php','返回');
        }
      } else {
        self::throwException('不存在该用户。');
        self::Button('viewAllUsers.php','返回');
      }
    }

    public static function Button($href,$text){
    	echo '<a href='.$href.'><input type="button" value='.$text.'></a>';
    }

    public static function getAdminRank($adminRank){
      if ($adminRank==0) return '普通用户';
      if ($adminRank==1) return '管理员';
      if ($adminRank==2) return '超级管理员';
    }

    public static function showUserInfo($id,$username,$email,$adminRank){
    	echo "这里是".$username."的博客。<br/>";
      self::showHeadPic($id);
      echo '<form action="actionModifyHeadPic.php" method="post" enctype="multipart/form-data">';
      echo '<input type="file" accept=".jpg" name="headPic"><input type="submit" value="上传头像"></form>';
    	echo "<br/>你的邮箱地址为：".$email;
    	self::Button('modifyEmail.php','修改邮箱地址');
    	echo '<br/>你的权限为：'.self::getAdminRank($adminRank).'<br/>';
      //self::Button('test.php','测试');
    }
  }
?>